package com.example.springbootdemo.scene.controller.user;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springbootdemo.controller.BaseController;
import com.example.springbootdemo.scene.entity.Scene;
import com.example.springbootdemo.scene.entity.ext.redismq.MarkScene;
import com.example.springbootdemo.scene.service.SceneService;
import com.example.springbootdemo.service.RedisService;
import com.example.springbootdemo.utils.PageUtils;
import com.example.springbootdemo.utils.WebResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 景区各种接口
 *
 * @author hou on 2020/11/19
 * 修改记录:
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/app/scene")
@Api(tags = "前台端 景区相关")
public class SceneController extends BaseController {
    @Autowired
    SceneService sceneService;
    @Autowired
    RedisService redisService;

    @ApiOperation("获取所有景区列表信息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", required = true, value = "当前页", paramType = "query"),
            @ApiImplicitParam(name = "limit", required = true, value = "一页显示的条数", paramType = "query"),
    })
    @GetMapping("/list")
    public WebResult list(Integer page, Integer limit, HttpServletRequest request) {
        QueryWrapper<Scene> queryWrapper = new QueryWrapper<Scene>().orderByDesc("gmt_modified");
        PageUtils<Scene> pageUtils = sceneService.queryForPage(page, limit, queryWrapper);
        if (pageUtils.getList() != null) {
            return data(pageUtils);
        }
        return fail("查询失败");
    }

    @ApiOperation("给景区打分,采用redis发布订阅做异步操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sceneId", required = true, value = "景区id", paramType = "query"),
            @ApiImplicitParam(name = "score", required = true, value = "分数", paramType = "query"),
    })
    @PutMapping("/mark-score")
    public WebResult markScore(@RequestBody MarkScene markScene, HttpServletRequest request) {
        log.info("markScene{}",JSON.toJSONString(markScene));
        redisService.sendMsg("markScene", JSON.toJSONString(markScene));
        return ok("打分成功");
    }

}
